package com.itheima.controller;

import com.itheima.dao.StudentDao;
import com.itheima.domain.Student;
import com.itheima.service.StudentService;

import java.util.Scanner;

public class StudentController {
    public static Scanner scanner = new Scanner(System.in);
    public static StudentService studentService = new StudentService();
    public static void main(String[] args) {
        lo:
        while (true) {
            // 1. 搭建主界面菜单
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.print("1 添加学生\t");
            System.out.print("2 删除学生\t");
            System.out.println("3 修改学生");
            System.out.print("4 查看学生\t");
            System.out.println("5 退出");
            System.out.print("请输入您的选择:");
            String choice = scanner.next();

            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //  System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    findAllStudent();
                    System.out.println("查看学生");
                    //  queryStudents(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }


    }
    //修改
    private static void updateStudent() {

        StudentDao Index = inputStudentId();
        Student student = inputStudentInfo();
        System.out.println("开始修改...");
        studentService.updateStudent(Index,student);
        System.out.println("修改成功");
    }
    //删除
    private static void deleteStudentById() {

        StudentDao deleteIndex = inputStudentId();
        System.out.println("开始删除...");
        studentService.deleteStudent(deleteIndex);
        System.out.println("删除成功");
    }

    public static StudentDao inputStudentId(){
        String id;
        StudentDao Index;
        while(true) {
            System.out.println("请输入id:");
            id = scanner.next();
            Index = studentService.isExists(id);
            if (Index == null) {
                System.out.println("该id不存在!");
            }else{
                return Index;
            }
        }
    }

    //查询
    private static void findAllStudent() {
        StudentDao head = studentService.findAllStudent();
        while (head.getNextIndex() != null) {
            String id = head.getNextIndex().getStudent().getId();
            String name = head.getNextIndex().getStudent().getName();
            String age = head.getNextIndex().getStudent().getAge();
            String birthday = head.getNextIndex().getStudent().getBirthday();

            System.out.println("ID: "+id+" 姓名: "+name+" 年龄: "+age+" 生日: "+birthday);
            head = head.getNextIndex();
        }
    }

    //添加
    private static void addStudent() {
        String id;
        StudentDao studentDao;
        while(true) {
            System.out.println("请输入id:");
            id = scanner.next();
             studentDao = studentService.isExists(id);
            if (studentDao == null) {
                break;
            }else{
                System.out.println("该id已被占用!");
            }
        }

        Student student = inputStudentInfo(id);
        studentService.addStudent(student);
    }

    public static Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名:");
        String name = scanner.next();
        System.out.println("请输入学生年龄:");
        String age = scanner.next();
        System.out.println("请输入学生生日:");
        String birthday = scanner.next();

        Student s = new Student(id,name,age,birthday);
        return s;
    }

    public static Student inputStudentInfo() {
        System.out.println("请输入学生Id:");
        String id = scanner.next();
        System.out.println("请输入学生姓名:");
        String name = scanner.next();
        System.out.println("请输入学生年龄:");
        String age = scanner.next();
        System.out.println("请输入学生生日:");
        String birthday = scanner.next();

        Student s = new Student(id,name,age,birthday);
        return s;
    }
}
